/**
 * PrivilegeController.java
 */
package org.yate.oa.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.yate.oa.model.PrivilegeEntity;
import org.yate.oa.service.IPrivilegeService;
import org.yate.oa.service.imp.PrivilegeService;
import org.yate.oa.utils.Pagination;

import com.alibaba.fastjson.JSONObject;

/**
 * @author Yate
 * @date Nov 18, 2013
 * @description TODO
 * @version 1.0
 */
@WebServlet(urlPatterns = { "/privilege/get", "/privilege/post"  }, asyncSupported = true)
public class PrivilegeController extends RestfulController {

	private IPrivilegeService privService;
	/* 
	 * (non-Javadoc)
	 * @see org.yate.oa.controller.BaseController#init(javax.servlet.ServletConfig)
	 */
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		this.privService = (IPrivilegeService) super.springCtx
				.getBean("privilegeService");
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
	}
	
	@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        String type = req.getParameter("type");
        if (type.equals("add"))
        {
            addPrivilege(req, res);
        }else if(type.equals("all")){
            getPriviles(req, res);
        }
    }
	
	protected void addPrivilege(HttpServletRequest req, HttpServletResponse res) throws IOException
    {
	    String privName = req.getParameter("privName");
	    String modId = req.getParameter("modId");
	    String privUrl = req.getParameter("privUrl");
	    String isTree = req.getParameter("isTree");
        PrintWriter out = res.getWriter();
        JSONObject json = new JSONObject();
        PrivilegeService privilegeService = this.springCtx.getBean(PrivilegeService.class);
//        PrivilegeEntity privilegeEntity = new PrivilegeEntity();
//        privilegeEntity.setPrivName(privName);
//        privilegeEntity = privilegeService.getEntityBy(privilegeEntity);
//        if (privilegeEntity == null)
//        {
//            privilegeEntity = new PrivilegeEntity();
//            privilegeEntity.setPrivName(privName);
//            privilegeEntity.setPrivUrl(privUrl);
//            privilegeEntity.setModId(Long.parseLong(modId));
//            privilegeEntity.setIsTree(Integer.parseInt(isTree));
//            privilegeService.add(privilegeEntity);
//            json.put("status", 200);
//        }
//        else
//        {
//            json.put("status", 201);
//            json.put("text", "名称已经存在");
//        }
        PrivilegeEntity privilegeEntity = new PrivilegeEntity();
        privilegeEntity.setPrivName(privName);
        privilegeEntity.setPrivUrl(privUrl);
        privilegeEntity.setModId(Long.parseLong(modId));
        privilegeEntity.setIsTree(Integer.parseInt(isTree));
        privilegeService.add(privilegeEntity);
        json.put("status", 200);
        
        out.println(json);
        out.flush();
        out.close();
    }
	
	protected void getPriviles(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    {
	    
	    PrintWriter out = res.getWriter();
        JSONObject json = new JSONObject();
        String page = req.getParameter("page");
        int pageIndex = 0;
        if (!StringUtils.isEmpty(page))
        {
            pageIndex = Integer.parseInt(page);
        }
        Pagination<PrivilegeEntity> data = this.privService.getEntities(pageIndex, 10, "", "");
        json.put("status", "200");
        json.put("data", data);
        out.println(json);
        out.flush();
        out.close();
    }
}
